local skel = fk.CreateSkill{
    name="wegame_2__xianshou",
}
local U = require "packages/utility/utility"


Fk:loadTranslationTable{
    ["wegame_2__xianshou"] = "险守",
    [":wegame_2__xianshou"] = [[你造成或受到伤害后，你须翻转判定区中的【兵临城下】，否则你可摸两张牌，并将一张手牌当做【兵临城下】扣置于判定区中，此牌暗置时为被蓄谋的普通锦囊牌，明置时为判定区的延时锦囊牌。]],
    ["#xianshou-use"] = "险守：请一张手牌当做【兵临城下】扣置于判定区中",
}

local filp_card =function (player,room,cards,skillname)
    for _,cid in ipairs(cards) do
        local card=Fk:getCardById(cid)
        if card.trueName == "enemy_at_the_gates" then
            room:setCardMark(card,skel.name,"wegame__xianshou_card")
            Fk:filterCard(card.id, player)
            room:useVirtualCard("wegame__xianshou_card", card, player, player, skillname)
        else
           player:chat("忍耐，就是要想得开，挺得住")
            player:removeVirtualEquip(cid)
            local bingbing=Fk:cloneCard("enemy_at_the_gates")
            bingbing.skillName=skillname
            bingbing:addSubcard(card)
            room:setCardMark(card,skel.name,"enemy_at_the_gates")
            Fk:filterCard(card.id, player)

            U.premeditate(player, bingbing, skillname)
            
        end
    end
end

local spec= {
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(self.name)
    end,
    on_cost=Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
       --判定区是否有兵临城下
       local judgecards=player:getCardIds("j")
       local noxunhuan=true
       if data.skillName and data.skillName == "wegame__xianshou_card" then
        noxunhuan=false
       end
       if noxunhuan and table.find(player.virtual_equips,function (card) return card.name == "wegame__xianshou_card" end) then
        local bingling={}
        --获得虚拟兵临
        for _, c in ipairs(player.virtual_equips) do
            if c.name == "wegame__xianshou_card" then
                for _, id in ipairs(c.subcards) do
                    table.insert(bingling,id)
                end
            end
       
        end
        filp_card(player,room,bingling,skel.name)
       else
            player:drawCards(2,skel.name)
            if player:isKongcheng() then return end
            local card=room:askToCards(player,{
                min_num = 1, max_num = 1, skill_name = skel.name, cancelable = false,
                prompt = "#xianshou-use",
            })
            room:useVirtualCard("wegame__xianshou_card", card, player, player, skel.name)
       end
    end,
}

skel:addEffect(fk.Damage,spec)
skel:addEffect(fk.Damaged,spec)


skel:addEffect("filter", {  
    card_filter = function(self, to_select, player, isJudgeEvent)
      return to_select:getMark(skel.name) ~= 0
    end,
    view_as = function(self, player, to_select)
      local name = to_select:getMark(skel.name)
      return Fk:cloneCard(name, to_select.suit, to_select.number)
    end,
  })



return skel